package com.wanxi.service.Impl;

import com.wanxi.dao.CustomerDao;
import com.wanxi.dao.Impl.CustomerDaoImpl;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.CustomerModel;
import com.wanxi.service.CustomerService;

/**
 * @author Li
 * @Date 2023/5/6 21:03
 */
public class CustomerServiceImpl implements CustomerService {
    private CustomerDao customerDao = new CustomerDaoImpl();
    public ResultDto add(CustomerModel customerModel) {
        boolean same = customerDao.findSameName(customerModel);
        if (same){
            return ResultDto.builder()
                    .code(0)
                    .msg("用户名已存在")
                    .build();
        }
        boolean samePhone = customerDao.findSamePhone(customerModel);
        if (samePhone){
            return ResultDto.builder()
                    .code(0)
                    .msg("电话号已存在")
                    .build();
        }

        boolean samePEmail= customerDao.findSameEmail(customerModel);
        if (samePEmail){
            return ResultDto.builder()
                    .code(0)
                    .msg("邮箱已存在")
                    .build();
        }
        return ResultDto.builder()
                .code(0)
                .msg("添加成功")
                .count(customerDao.add(customerModel))
                .build();
    }

    public ResultDto update(CustomerModel customerModel) {
//        boolean same = customerDao.findSameName(customerModel);
//        if (same){
//            return ResultDto.builder()
//                    .code(0)
//                    .msg("用户名已存在")
//                    .build();
//        }
        return ResultDto.builder()
                .code(0)
                .msg("修改成功")
                .count(customerDao.update(customerModel))
                .build();
    }

    public ResultDto delete(CustomerModel customerModel) {
        return ResultDto.builder()
                .code(0)
                .msg("删除成功")
                .count(customerDao.delete(customerModel))
                .build();
    }

    public ResultDto findAll(CustomerModel customerModel) {
        return ResultDto.builder()
                .code(0)
                .msg("查询成功")
                .count(customerDao.count(customerModel))
                .data(customerDao.findAll(customerModel))
                .build();
    }

    public ResultDto findById(CustomerModel customerModel) {
        return ResultDto.builder()
                .code(0)
                .msg("查询成功")
                .data(customerDao.findById(customerModel))
                .build();
    }

    @Override
    public Integer totalCount() {
        return customerDao.totalCount();
    }
}
